package com.mcb_wxc.util;


import taobe.tec.jcc.JChineseConvertor;

import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by 小生
 * 2019/3/27 21:00.
 */

public class SimpleToNumerous {

    public static void main(String[] args) {
        String filePath = selectFilePath("E:\\");
        if(filePath == null) {
            return;
        }
        //"E:\\dd\\src\\main\\java\\com\\dd\\mapper"
        updateFileSuffix(filePath, ".php", ".java");
        changeFileFromSimpleChineseToTradionalWithRootPath(filePath);
        updateFileSuffix(filePath,".java", ".php");

    }


    /**
     * 转换所以文件夹下的文件后缀
     * @param path 文件夹
     * @param oldFileSuffix  如 .php
     * @param newFileSuffix 如 .java
     */
    public static void updateFileSuffix(String path, String oldFileSuffix, String newFileSuffix) {
        File file = new File(path);
        //得到文件夹下的所有文件和文件夹
        String[] list = file.list();

        if(list!=null && list.length>0){
            for (String oldName : list) {
                File oldFile = new File(path,oldName);
                //判断出文件和文件夹
                if(!oldFile.isDirectory()){
                    //文件则判断是不是要修改的
                    if(oldName.contains(oldFileSuffix)){
                        System.out.println(oldName);
                        String newoldName = oldName.substring(0, oldName.lastIndexOf("."))+newFileSuffix;
                        System.out.println(newoldName);
                        File newFile = new File(path,newoldName);
                        boolean flag = oldFile.renameTo(newFile);
                        System.out.println(flag);
                    }
                }else{
                    //文件夹则迭代
                    String newpath=path+"/"+oldName;
                    updateFileSuffix(newpath,oldFileSuffix,newFileSuffix);
                }
            }
        }
    }


    /**
     * 选文件
     * @param initPath 初始化盘符路径 如："E:\"
     * @return
     */
    public static String selectFilePath(String initPath) {
        //第一步选择文件
        String filePath = null;
        //可设置初始化弹的盘
        JFileChooser fileChooser = new JFileChooser(initPath);
        //FILES_AND_DIRECTORIES 标识选择文件的类型
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int returnVal = fileChooser.showOpenDialog(fileChooser);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            filePath = fileChooser.getSelectedFile().getAbsolutePath();//选择的文件夹的路径
        }
        return filePath;
    }


    /**
     * 转这个文件下的所有文字位繁体
     * @param path
     */
    public static void changeFileFromSimpleChineseToTradionalWithRootPath(String path){
        ArrayList<String> tempArray = new ArrayList<String>();
        ArrayList<String> fileList = traverseFolder2(path , tempArray);
        System.out.println("文件数组" + fileList);

        if (fileList.size()==0){return;};
        for (int i = 0; i<fileList.size() ; i++){
            readOldFileAndWriteNewFileWithFilePath(fileList.get(i));
        }
    }


    public static void readOldFileAndWriteNewFileWithFilePath(String filePath){
        // 简体转繁体 
        try{
            BufferedReader bufRead = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));
            StringBuffer strBuffer = new StringBuffer();

            for (String temp = null;(temp = bufRead.readLine())!= null;temp = null ){
                Pattern pattern = Pattern.compile("[\u4e00-\u9fcc]+");
                if (pattern.matcher(temp).find()){
                    temp = getChinese(temp);
                }
                strBuffer.append(temp);
                strBuffer.append(System.getProperty("line.separator"));
            }
            System.out.println(strBuffer.toString());
            bufRead.close();
            PrintWriter printWriter = new PrintWriter(filePath);
            printWriter.write(strBuffer.toString().toCharArray());
            printWriter.flush();
            printWriter.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }




    /**把读取的文件的每一行字符串进行正则匹配简体中文
     * 并且把匹配到的简体中文替换为繁体
     * 并返回替换后的字符串
     * paramValue：读文件时候，读取到的每一行字符串*/
    public static String getChinese(String paramValue) {
        String regex = "([\u4e00-\u9fa5]+)";
        String replacedStr = paramValue;
        Matcher matcher = Pattern.compile(regex).matcher(paramValue);
        while (matcher.find()) {
            System.out.println("----------"+matcher.group(0));
            String traditionalStr = simplifiedCharToTraditionalChar(matcher.group(0));
            System.out.println(traditionalStr);

            replacedStr = replacedStr.replace(matcher.group(0),traditionalStr);

            System.out.println("zyf" +  traditionalStr  +  replacedStr);
        }
        return replacedStr;
    }



    /**
     * 简体中文转为繁体中文
     * @param str
     * @return
     */
    public static String simplifiedCharToTraditionalChar(String str) {
        if(str != null){
            try {
                JChineseConvertor jChineseConvertor = JChineseConvertor.getInstance();
                str = jChineseConvertor.s2t(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;
    }

    /**
     * 繁体中文转为简体中文
     * @param str
     * @return
     */
    public static String traditionalCharToSimplifiedChar(String str) {
        if(str != null){
            try {
                JChineseConvertor jChineseConvertor = JChineseConvertor.getInstance();
                str = jChineseConvertor.t2s(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;
    }


    /**迭代遍历传入的根文件夹，获取每一级文件夹的每个文件
     * 并把文件名称以字符串形式装在数组返回
     * path：根文件夹路径
     * listFileName：用于返回文件路径的数组，由于这个是迭代方法采用外部传入该数组 */
    public static ArrayList<String> traverseFolder2(String path , ArrayList<String> listFileName ) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length == 0) {
                System.out.println("文件夹是空的!");
                return null;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        System.out.println("文件夹:" + file2.getAbsolutePath());
                        traverseFolder2(file2.getAbsolutePath(),listFileName);
                    } else {
                        String sbsolutePath = file2.getAbsolutePath();
                        if (sbsolutePath.endsWith(".jsp") || sbsolutePath.endsWith(".js") || sbsolutePath.endsWith(".html") || sbsolutePath.endsWith(".java") ){
                            listFileName.add(file2.getAbsolutePath());
                        }

                        System.out.println("文件:" + file2.getAbsolutePath());
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }

        return listFileName;
    }
}
